/*
 To change this template, choose Tools | Templates
 and open the template in the editor.
 */
package network;

import java.util.Hashtable;
import java.util.Vector;

import lib.Core;
import lib.Money;
import lib.XmlPullParser;
import Core.Contants;
import Core.GlobalData;
import DTO.Balance;
import DTO.Good;
import DTO.Group;
import DTO.Provider;
import DTO.PurchaseOnline;
import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;

import com.simpay.cashierandroidapp.DialogScreen;
import com.simpay.login.CreatePincodeActivity;
import com.simpay.login.MainActivity;

/*
 *
 * @author HOME
 */
@SuppressLint("UseValueOf")
public class Packet {

	public static final byte TYPE_GETAGENTS = 1;
	public static final byte TYPE_GETAGENTINFO = 2;
	public static final byte TYPE_GETCOMMISSIONS = 3;
	public static final byte TYPE_GETCOMMISSIONPROFILES = 4;
	public static final byte TYPE_GETRESULTCODES = 5;
	public static final byte TYPE_GETUIPROVIDERS = 6;
	public static final byte TYPE_GETLASTIDS = 7;
	public static final byte TYPE_ADDOFFLINEPAYMENT = 8;
	public static final byte TYPE_GETPAYMENTSTATUS = 9;
	public static final byte TYPE_GETNEWS = 11;
	public static final byte TYPE_GETSTATISTIC = 12;
	public static final byte TYPE_SETPUBLICKEY = 13;
	public static final byte TYPE_CHECKMESSAGES = 14;
	public static final byte TYPE_GETMESSAGES = 15;
	public static final byte TYPE_TRANSFER_FUNDS = 16;
	public static final byte TYPE_SENDPAYMENTS = 17;
	public static final byte TYPE_GETBALANCE_LOGIN = 18;
	public static final byte TYPE_GETBALANCE_UPDATE = 26;
	public static final byte TYPE_CANCELPAYMENT = 19;
	public static final byte TYPE_GETGROUPS = 20;
	public static final byte TYPE_AUTHENTICATION = 21;
	public static final byte TYPE_GETPROVIDERS = 22;
	public static final byte TYPE_GETNOMENCLATURE = 24;
	public static final byte TYPE_PURCHASEONLINE = 25;
	public static final byte TYPE_HEADNUMBER = 27;
	public static final byte TYPE_PRICETYPES = 28;
	public static final byte TYPE_CHANGEPASS = 29;
	public static final byte TYPE_CHECKREQUISITES = 30;
	public static final byte TYPE_SENDMAIL = 31;
	public static final byte TYPE_LOADLOGO = 32;
	private int packet;
	CreateXMLProvider createXMLProvider;
	ParseXMLProvider parseXMLProvider;
	Object data;
	private Context context;
	private LoadViewTask loadViewTask;

	public Packet(int type, Context next, LoadViewTask loadViewTask) {
		this.packet = type;
		this.context = next;
		createXMLProvider = new CreateXMLProvider();
		parseXMLProvider = new ParseXMLProvider();
		this.loadViewTask = loadViewTask;

	}

	public Packet(int type, Context next, Object data, LoadViewTask loadViewTask) {
		this.packet = type;
		this.context = next;
		this.data = data;
		parseXMLProvider = new ParseXMLProvider(data);
		this.loadViewTask = loadViewTask;
	}

	public String toXML() {
		createXMLProvider = new CreateXMLProvider(data);
		parseXMLProvider = new ParseXMLProvider(data);
		return createXMLProvider.toXML(packet);
	}

	public void parseXML(XmlPullParser xml) throws Exception {
		switch (packet) {

		case TYPE_GETBALANCE_LOGIN: {
			// 1___Set balance
			Balance balance = (Balance) parseXMLProvider.parseBalance(xml);
			if (balance == null) {
				// //AppEntity.event(AppEntity.E_ERR_INFOLOGIN, null);
				return;
			}
			GlobalData.balance = balance;
			GlobalData.currentBalance = balance.getBalance();
			Core.writeData(Contants.BALANCE, balance);

			// //AppEntity.event(AppEntity.E_PINCODE_CREATE);
			// AppEntity.event(AppEntity.E_UPDATE_BALANCE, next);
		}
			break;
		case TYPE_GETBALANCE_UPDATE: {
			Balance balance = (Balance) parseXMLProvider.parseBalance(xml);
			if (balance != null) {
				GlobalData.balance = balance;
				GlobalData.currentBalance = balance.getBalance();
				Core.writeData(Contants.BALANCE, balance);
			}

			DialogScreen.show("Số dư Tài khoản:"
					+ Money.toStringDelDot(GlobalData.balance.getBalance())
					+ " VND");
		}
			break;
		case TYPE_SETPUBLICKEY: {
			int resultCode = parseXMLProvider.parseSetPublicKey(xml);
			if (resultCode == 0) {
				GlobalData.firstStart = false;
				Core.writeData(Contants.ISFIRSTSTART, false);

				Intent i = new Intent(context, CreatePincodeActivity.class);
				context.startActivity(i);

			} else if (resultCode == 150) {
				DialogScreen
						.show("Lỗi #150: Thông tin đăng nhập không chính xác");
			} else if (resultCode == 151) {
				DialogScreen.show("Lỗi #151: Mật khẩu đăng nhập một lần");
			}
		}

			break;

		case TYPE_GETGROUPS: {
			Vector<Group> groups = parseXMLProvider.parseGroups(xml);

			int count_orderID = 12;
			for (int i = 0; i < groups.size(); i++) {
				if (((Group) groups.elementAt(i)).getId() == 20) {
					((Group) groups.elementAt(i)).orderId = 1;
				} else if (((Group) groups.elementAt(i)).getId() == 826) {
					((Group) groups.elementAt(i)).orderId = 2;
				} else if (((Group) groups.elementAt(i)).getId() == 103) {
					((Group) groups.elementAt(i)).orderId = 3;
				} else if (((Group) groups.elementAt(i)).getId() == 817) {
					((Group) groups.elementAt(i)).orderId = 4;
				} else if (((Group) groups.elementAt(i)).getId() == 816) {
					((Group) groups.elementAt(i)).orderId = 5;
				} else if (((Group) groups.elementAt(i)).getId() == 123) {
					((Group) groups.elementAt(i)).orderId = 6;
				} else if (((Group) groups.elementAt(i)).getId() == 822) {
					((Group) groups.elementAt(i)).orderId = 7;
				} else if (((Group) groups.elementAt(i)).getId() == 824) {
					((Group) groups.elementAt(i)).orderId = 8;
				} else if (((Group) groups.elementAt(i)).getId() == 825) {
					((Group) groups.elementAt(i)).orderId = 9;
				} else if (((Group) groups.elementAt(i)).getId() == 823) {
					((Group) groups.elementAt(i)).orderId = 10;
				} else {
					((Group) groups.elementAt(i)).orderId = count_orderID;
					count_orderID++;
				}
			}

			if (groups != null) {
				for (int i = 0, length = groups.size(); i < length - 1; i++) {
					for (int j = i + 1; j < length; j++) {
						Group op1 = (Group) groups.elementAt(i);
						Group op2 = (Group) groups.elementAt(j);
						if (op1.orderId > op2.orderId) {
							groups.removeElementAt(i);
							groups.insertElementAt(op2, i);
							groups.removeElementAt(j);
							groups.insertElementAt(op1, j);
						}
					}
				}
			}

			Core.writeData(Contants.GROUPS, groups);
			new LoadViewTask(Packet.TYPE_GETPROVIDERS, context);
		}
			break;
		case TYPE_GETPROVIDERS: {
			Vector<Provider> v = parseXMLProvider.parseProviders(xml);
			Core.writeData(Contants.PROVIDERS, v);

			new LoadViewTask(Packet.TYPE_GETRESULTCODES, context);

		}
			break;
		case TYPE_GETRESULTCODES: {
			Hashtable<Integer, String> res = parseXMLProvider
					.parseGetResultCodes(xml);
			GlobalData.errCodeTable = res;
			Core.writeData(Contants.TABLE_CODE, res);

			new LoadViewTask(Packet.TYPE_HEADNUMBER, context);
		}
			break;

		//
		case TYPE_SENDPAYMENTS:
			parseXMLProvider.parseAddOfflinePayment(xml);
			break;
		//
		case TYPE_GETPAYMENTSTATUS: {
			int status = parseXMLProvider.parseGetOfflinePaymentStatus(xml);
			if (status != 1) {
				// new
				// NetworkThread(Packet.TYPE_GETBALANCE_UPDATE,//AppEntity.E_UPDATE_BALANCE);
			}
		}
			break;
		case TYPE_GETMESSAGES: {
			parseXMLProvider.parseMessage(xml);
			// AppEntity.event(AppEntity.E_UPDATE_MESSAGE, next);

		}
			break;
		//
		case TYPE_GETNOMENCLATURE: {
			Vector<Good> nomenClatures = parseXMLProvider
					.parseGetNomenclature(xml);
			if (nomenClatures != null) {
				for (int i = 0, length = nomenClatures.size(); i < length - 1; i++) {
					for (int j = i + 1; j < length; j++) {
						Good op1 = (Good) nomenClatures.elementAt(i);
						Good op2 = (Good) nomenClatures.elementAt(j);
						if (op1.getAmount() > op2.getAmount()) {
							nomenClatures.removeElementAt(i);
							nomenClatures.insertElementAt(op2, i);
							nomenClatures.removeElementAt(j);
							nomenClatures.insertElementAt(op1, j);
						}
					}
				}
			}
			// Core.setObjectToRms(Contants.NOMENCLATURE, nomenClatures);
		}
			break;
		case TYPE_PURCHASEONLINE: {
			PurchaseOnline po = parseXMLProvider.parsePurchaseOnline(xml);
			// AppEntity.event(AppEntity.E_UPDATE_PURCHASEONLINE,
			// AppEntity.getObjects(po));

		}
			break;
		//
		case TYPE_HEADNUMBER: {
			parseXMLProvider.parseHeadNumber(xml);

			new LoadViewTask(Packet.TYPE_PRICETYPES, context);
		}
			break;
		case TYPE_PRICETYPES: {
			parseXMLProvider.parsePriceTypes(xml);
			new LoadViewTask(Packet.TYPE_LOADLOGO, context);
		}
			break;
		

		}

	}
}
